package com.zhouk.httpClient;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Create By Zhouke on 2017/08/28
 */
public class Spider {
    private static Logger logger = LoggerFactory.getLogger(Spider.class);
    public static Document getHtmlByUrl(String url){
        Document doc = null;
        Map<String, String> datas = new HashMap<>();
        Map<String, String> cookies = new HashMap<>();

        cookies.put("acf_uid","38930144");
        cookies.put("acf_auth","4TLbdCkkAct%2FVfpQt8d0GzHojqf95IzelhJSAABDbtPaW");


        try {
            long delayTime = (long) (Math.random() * 2000);  //随机延迟时间
            Thread.sleep(delayTime);
            logger.info("随机延迟{}毫秒,爬取URL:{}", delayTime, url);
            doc = Jsoup.connect(url)
                    .data(datas)                   //参数
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36")                 //用户头
                    .cookies(cookies)
                    //.cookie("test","test")  //cookie
                    .timeout(300000)               //超时时间
                    .post();                       //post方式
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("网络错误，无法请求{}" + e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return doc;
    }


    public static Document getHtmlByFile(String path){
        Document doc = null;
        try {
            File htmlFile = new File(path);
            if(htmlFile.isFile()) {
                logger.info("读取本地html文件:{}", path);
                doc = Jsoup.parse(htmlFile, "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }

    public static void saveHtmlToFile(String url){

    }
}
